//
//  CLHViewController.m
//  CLHComponents
//
//  Created by clh2046@163.com on 03/16/2020.
//  Copyright (c) 2020 clh2046@163.com. All rights reserved.
//

#import "CLHViewController.h"
#import <CLHComponents/CLHComponents.h>
#import "ComponentScrollViewVC.h"

@interface CLHViewController ()<ContentSearchViewDelegate,FormComponentViewDelegate,ConfiGurableTextViewDelegate>

@property (nonatomic, strong) UIScrollView *mainScrollView;

@end

@implementation CLHViewController

- (UIScrollView *)mainScrollView {
    if (!_mainScrollView) {
        _mainScrollView =[[UIScrollView alloc]initWithFrame:self.view.bounds];
        _mainScrollView.backgroundColor = [UIColor grayColor];
    }
    return _mainScrollView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.view addSubview:self.mainScrollView];
    
    WaterMarkView *waterMarkView = [[WaterMarkView alloc]initWithFrame:CGRectMake(50, 50, [UIScreen mainScreen].bounds.size.width - 100, 100) withText:@"水印" textFont:[UIFont systemFontOfSize:12] textColor:[UIColor redColor] h_space:30 v_space:30];
    waterMarkView.backgroundColor = [UIColor yellowColor];
    [self.mainScrollView addSubview:waterMarkView];
    
    ContentSearchView *search = [[ContentSearchView alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(waterMarkView.frame) + 20, [UIScreen mainScreen].bounds.size.width - 100, 45) showType:ContentShowTypeIconRightBtn placeholder:@"搜一搜" delegate:self];
    search.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView addSubview:search];
    
    DataRoundLoopView *loopView = [[DataRoundLoopView alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(search.frame) + 20, 167, 167) lineWidth:20];
    [loopView setIsSpace:YES];
    [loopView updateWithModels:@[[[DataRoundLoopModel alloc]initWithDictionary:@{@"name":@"name1",@"value":@(2),@"total":@(10),@"color":[UIColor yellowColor]}],[[DataRoundLoopModel alloc]initWithDictionary:@{@"name":@"name2",@"value":@(3),@"total":@(10),@"color":[UIColor redColor]}],[[DataRoundLoopModel alloc]initWithDictionary:@{@"name":@"name3",@"value":@(2),@"total":@(10),@"color":[UIColor blueColor]}],[[DataRoundLoopModel alloc]initWithDictionary:@{@"name":@"name3",@"value":@(3),@"total":@(10),@"color":[UIColor greenColor]}]]];
    [self.mainScrollView addSubview:loopView];
    
    FormComponentView *formComponentView = [[FormComponentView alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(loopView.frame)+20, 200, 150)];
    formComponentView.delegate = self;
    [formComponentView updateFormViewWithDataArray:[self formData]];
    [self.mainScrollView addSubview:formComponentView];
    
    ShadowView *shadowView = [[ShadowView alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(formComponentView.frame) + 20, 200, 200)];
    [shadowView yc_cornerRadius:10];
    [shadowView yc_shaodwRadius:10 shadowColor:[UIColor blueColor] shadowOffset:CGSizeMake(10, 10) byShadowSide:YCShadowSideBottom|YCShadowSideRight];
    [self.mainScrollView addSubview:shadowView];
    
    LineationLabel *label = [[LineationLabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(shadowView.frame)+20, [UIScreen mainScreen].bounds.size.width, 40)];
    label.backgroundColor = [UIColor groupTableViewBackgroundColor];
    label.text = @"1234567891011121314";
    label.textColor = [UIColor redColor];
    label.lineColor = [UIColor blueColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.lineType = LineationLabelLineTypeMiddle;
    [self.mainScrollView addSubview:label];
    
    ShadeRoundLoopView *roundLoopView = [[ShadeRoundLoopView alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(label.frame) + 20, 78, 78)];
    roundLoopView.reduceAngle = 0;
    roundLoopView.tag = 111099;
    roundLoopView.progressLoopWidth = 10;
    roundLoopView.backLoopWidth = 10;
    roundLoopView.backLoopColor = [UIColor colorWithRed:239.0/255.0 green:242.0/255.0 blue:255.0/255.0 alpha:1];
    roundLoopView.startAngle = 0;
    roundLoopView.reduceAngle = 0;
    roundLoopView.progress = 0.0;
    roundLoopView.duration = 0.5;
    roundLoopView.autoAnimation = NO;
    roundLoopView.increaseFromLast = YES;
    roundLoopView.progressColors = @[[UIColor redColor],[UIColor yellowColor],[UIColor blueColor]];
    [self.mainScrollView addSubview:roundLoopView];
    
    ConfiGurableTextView * confiTextView = [[ConfiGurableTextView alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(roundLoopView.frame) + 20, [UIScreen mainScreen].bounds.size.width - 100, 322)supportAttributed:YES];
    confiTextView.returnShouldResignFirstResponder = YES;
    confiTextView.dynamicHeight = YES;
    confiTextView.maxLength = 50;
    confiTextView.banBlank = YES;
    confiTextView.banEmoji = YES;
    confiTextView.banLineFeed = NO;
    confiTextView.confiDelegate = self;
    confiTextView.selectedStrUrlDic = @{@"后端拓展点定制能力说明文档":@"https://www.baidu.com",
                                        @"店铺可通过此页面配置相关开关":@"https://www.jianshu.com"
    };
    confiTextView.selectedAttributeDic = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blueColor]};
    NSAttributedString *title0 = [self attributedTitleString:@"功能说明："];
    NSAttributedString *content0 = [self attributedContentString:@"店铺可通过此页面配置相关开关，将扩展点调用权限授权给外部应用接管的方式，实现定制化的会员，积分，余额，优惠券，批价等。\n"];
    NSAttributedString *title1 = [self attributedTitleString:@"关联标识："];
    NSAttributedString *content1 = [self attributedContentString:@"店铺可通过选择某个关联标识，作为跟外部打通数据的依据，目前提供微信用户unionId和phone两种方式。\n"];
    NSAttributedString *title2 = [self attributedTitleString:@"业务扩展通道："];
    NSAttributedString *content2 = [self attributedContentString:@"店铺可通过打开此开关标识当前店铺为使用后端扩展点的店铺，且当下方开关任意开关打开时，会自动打开业务扩展通道。店铺可通过选择关联对应的外部应用，来开启对应配置点的能力，开启配置点后，相应扩展点调用将会托管给外部应用服务。\n"];
    NSAttributedString *title3 = [self attributedTitleString:@"配置说明："];
    NSAttributedString *content3 = [self attributedContentString:@"各配置点实现的定制能力不同，具体请参考《后端拓展点定制能力说明文档》\n"];
    
    NSMutableAttributedString *maStr = [[NSMutableAttributedString alloc]init];
    [maStr appendAttributedString:title0];
    [maStr appendAttributedString:content0];
    [maStr appendAttributedString:title1];
    [maStr appendAttributedString:content1];
    [maStr appendAttributedString:title2];
    [maStr appendAttributedString:content2];
    [maStr appendAttributedString:title3];
    [maStr appendAttributedString:content3];
    
    confiTextView.attributedString = maStr;
    [self.mainScrollView addSubview:confiTextView];
    
    [self.mainScrollView setContentSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGRectGetMaxY(confiTextView.frame) + 1000)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"右" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"左" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
}

#pragma mark - ContentSearchViewDelegate
- (void)searchTitle:(NSString *_Nonnull)title searchType:(TextFieldSearchType)searchType {
    
}
#pragma mark - CustomTextViewDelegate
- (void)titleDidClicked:(ConfiGurableTextView *)textView {
//    [self showLoadingWithMessage:textView.customTextView.text hideAfter:2.5];
    [self.navigationController pushViewController:[ComponentScrollViewVC new] animated:YES];
}
- (void)titleDidClickedString:(NSString *)string urlString:(NSString *)urlString {
    NSLog(@"点击了- %@ 链接 - %@",string,urlString);
    [self.navigationController pushViewController:[ComponentScrollViewVC new] animated:YES];
}
#pragma mark - FormComponentViewDelegate
- (void)didTapFormItemWithModel:(LMRModel *)model
                          label:(LMRLabel *)label
                     reportView:(LMReportView *)reportView {
    NSLog(@"model.text");
}

- (void)didLongPressFormItemWithModel:(LMRModel *)model
                                label:(LMRLabel *)label
                           reportView:(LMReportView *)reportView {
    
}
#pragma mark - ConfiGurableTextViewDelegate
- (BOOL)textViewShouldReturn:(UITextView *)textView {
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
    NSLog(@"textView.text = %@",textView.text);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    
}

- (NSMutableArray<NSMutableArray<LMRModel*>*> *)formData {
    
    NSMutableArray *arr = [NSMutableArray<NSMutableArray<LMRModel*>*> array];
    NSMutableArray *row0 = [NSMutableArray<LMRModel*> array];
    [arr addObject:row0];
   
    LMRModel *model00 = [LMRModel modelTitle:@"0_0" size:CGSizeMake(60, 40)];
    LMRModel *model01 = [LMRModel modelTitle:@"0_1" size:CGSizeMake(60, 40)];
    LMRModel *model02 = [LMRModel modelTitle:@"0_2" size:CGSizeMake(60, 40)];
   
    [row0 addObject:model00];
    [row0 addObject:model01];
    [row0 addObject:model02];
    
    NSMutableArray *row1 = [NSMutableArray<LMRModel*> array];
    [arr addObject:row1];
    
    LMRModel *model10 = [LMRModel modelTitle:@"1_0" size:CGSizeMake(60, 40)];
    LMRModel *model11 = [LMRModel modelTitle:@"1_1" size:CGSizeMake(60, 40)];
    LMRModel *model12 = [LMRModel modelTitle:@"1_2" size:CGSizeMake(60, 40)];
    
    [row1 addObject:model10];
    [row1 addObject:model11];
    [row1 addObject:model12];
    
    NSMutableArray *row2 = [NSMutableArray<LMRModel*> array];
    [arr addObject:row2];
    
    LMRModel *model20 = [LMRModel modelTitle:@"2_0" size:CGSizeMake(60, 40)];
    LMRModel *model21 = [LMRModel modelTitle:@"2_1" size:CGSizeMake(60, 40)];
    LMRModel *model22 = [LMRModel modelTitle:@"2_2" size:CGSizeMake(60, 40)];
    
    [row2 addObject:model20];
    [row2 addObject:model21];
    [row2 addObject:model22];
    
    return arr;
}
- (void)rightBarButtonItemAction:(UIBarButtonItem *)ba {
    ShadeRoundLoopView *roundLoopView = (ShadeRoundLoopView *)[self.view viewWithTag:111099];
    roundLoopView.progress = roundLoopView.progress + 0.1;
    
}
- (void)leftBarButtonItemAction:(UIBarButtonItem *)ba {
    ShadeRoundLoopView *roundLoopView = (ShadeRoundLoopView *)[self.view viewWithTag:111099];
    roundLoopView.progress = roundLoopView.progress - 0.1;
}
- (NSAttributedString *)attributedTitleString:(NSString *)title {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:5];
    NSAttributedString * titleAtt = [[NSAttributedString alloc]initWithString:title attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:[UIColor grayColor],NSParagraphStyleAttributeName:paragraphStyle}];
    return titleAtt;
}
- (NSAttributedString *)attributedContentString:(NSString *)content {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:5];
    NSAttributedString * titleAtt = [[NSAttributedString alloc]initWithString:content attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor grayColor],NSParagraphStyleAttributeName:paragraphStyle}];
    return titleAtt;
}

@end
